package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.entity.CrBidPerson;
import com.arong.swing.db.entity.CrCheckevent;
import com.arong.swing.db.entity.CrPersionRecord;
import com.arong.swing.db.entity.PagingInfo;
import com.arong.swing.db.entity.PagingResult;
import com.arong.swing.db.entity.SearchMan;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;

public class SearchManSDAO extends BaseDAO {

    private String normal = "正常";

    public SearchManSDAO() {
        this(DB_CHECKRECORD);
    }

    public SearchManSDAO(String dbFile) {
        super(dbFile);
    }

    @Override
    public String getCreateTableSql() {
        return null;
    }

    /**
     * 分页查询
     * 
     * @param sql
     * @param pagingInfo
     * @param entity
     * @return
     * @throws SysException
     *             , AppException
     */
    public PagingResult<SearchMan> queryList(PagingInfo pagingInfo,
            CrPersionRecord persion, CrBidPerson bidPersion, CrCheckevent event)
            throws SysException, AppException {
        PagingResult<SearchMan> res = null;
        try {
            res = jdbcUtils.queryList(getSql(persion, bidPersion, event),
                    SearchMan.class, pagingInfo,
                    getParams(persion, bidPersion, event));
        } catch (SQLException e) {
            throw new SysException("queryList分页查询 error", e);
        }
        return res;
    }

    private String getSql(CrPersionRecord persion, CrBidPerson bidPersion,
            CrCheckevent event) {
        StringBuffer sql = new StringBuffer(
                "SELECT p.CHECKEVENT_ID,p.ID,p.SFHM,p.XM,p.MZ,p.XB,p.PCSJ,");
        sql.append(" e.USER_ID,e.CHECK_USER,b.RYLB,b.TASK_NAMES "
                + "from CR_PERSION_RECORD p left join  CR_CHECKEVENT e on p.CHECKEVENT_ID = e.ID  left join CR_BID_PERSON b  on p.ID = b.HCJL_ID "
                + "where 1=1");

        if (StringUtil.isNotBlank(bidPersion.getRylb())) {
        	if (normal.equals(bidPersion.getRylb())) {
                sql.append(" and b.RYLB is null ");
            } else {
            	sql.append(" and b.RYLB is not null ");
            }
        }
        if (StringUtil.isNotBlank(persion.getSfhm())) {
            sql.append(" and p.SFHM=?");
        }
        if (StringUtil.isNotBlank(persion.getXm())) {
            sql.append(" and p.XM=?");
        }
        if (event.getStartTime() != null) {
            sql.append(" and p.PCSJ>=?");
        }
        if (event.getEndTime() != null) {
            sql.append(" and p.PCSJ <=?");
        }
        if (StringUtil.isNotBlank(event.getUserId())) {
            sql.append(" and e.USER_ID=?");
        }

        sql.append(" order by p.PCSJ desc");
        return sql.toString();

    }

    private Object[] getParams(CrPersionRecord persion, CrBidPerson bidPersion,
            CrCheckevent event) {
        List<Object> qryList = new ArrayList<Object>();
        // if (StringUtil.isNotBlank(bidPersion.getRylb())) {
        // qryList.add(bidPersion.getRylb());
        // }
        if (StringUtil.isNotBlank(persion.getSfhm())) {
            qryList.add(persion.getSfhm());
        }
        if (StringUtil.isNotBlank(persion.getXm())) {
            qryList.add(persion.getXm());
        }
        if (event.getStartTime() != null) {
            qryList.add(event.getStartTime());
        }
        if (event.getEndTime() != null) {
            qryList.add(event.getEndTime());
        }
        if (StringUtil.isNotBlank(event.getUserId())) {
            qryList.add(event.getUserId());
        }
        return jdbcUtils.setQryList(qryList);
    }

}
